package com.frank.oj.controller.admin;


import com.frank.model.entity.training.TrainingCategory;
import com.frank.oj.common.exception.StatusFailException;
import com.frank.oj.common.result.CommonResult;
import com.frank.oj.service.admin.training.AdminTrainingCategoryService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequiresAuthentication
@RequiresRoles(value = {"root", "problem_admin"}, logical = Logical.OR)
@RequestMapping("/api/admin/training/category")
public class AdminTrainingCategoryController {

    @Resource
    private AdminTrainingCategoryService adminTrainingCategoryService;

    @PostMapping("")
    public CommonResult<TrainingCategory> addTrainingCategory(@RequestBody TrainingCategory trainingCategory) {
        try {
            return CommonResult.successResponse(adminTrainingCategoryService.addTrainingCategory(trainingCategory));
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @PutMapping("")
    public CommonResult<Void> updateTrainingCategory(@RequestBody TrainingCategory trainingCategory) {
        try {
            adminTrainingCategoryService.updateTrainingCategory(trainingCategory);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }

    @DeleteMapping("")
    public CommonResult<Void> deleteTrainingCategory(@RequestParam("cid") Long cid) {
        try {
            adminTrainingCategoryService.deleteTrainingCategory(cid);
            return CommonResult.successResponse();
        } catch (StatusFailException e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }
}